package day01;

import java.lang.reflect.Constructor;

/**
 * 懒汉模式 双重检查锁
 * @Author chenyeqing
 * @Date 2021/6/30 22:43
 */
public class SingletonDoubleCheck {
    /**
     * volatile 防止指令重排
     */
    private volatile static SingletonDoubleCheck singleton;
    private SingletonDoubleCheck() {
        System.out.println(Thread.currentThread().getName() + "ok");
    }

    public static SingletonDoubleCheck getSingleton() {
        // 双重检查锁
        if (singleton == null) {
            synchronized (SingletonDoubleCheck.class) {
                if (singleton == null) {
                    singleton = new SingletonDoubleCheck();
                }
            }
        }
        return singleton;
    }

}
